<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;

use App\Org\Image; 

use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     *    创建添加界面
     */
    public function getAdd()
    {
        $res = DB::table('project_type')
            ->select(DB::raw('*,concat(path,id) as paths'))
            ->orderby('paths')
            ->get();

        foreach($res as $k=>$v) {
            //以逗号进行分割
            $path = explode(',',$v->path);
            //获取逗号的数量
            $len = count($path)-2;
            //根据逗号的数量进行重复
            $v->typename = str_repeat('--|',$len).$v->typename;
        }

        return view('/admin/goods/add',['res'=>$res]);
    }

    /**
     *    创建添加方法
     */
    public function postInsert(Request $request)
    {

        $res = $request->except('_token');
        if($request->hasFile('goodspic')){
            //随机号
            $name = md5(rand(10000,99999).time());
            //后缀名
            $suffix = $request->file('goodspic')->getClientOriginalExtension();

            $filename = $name.'.'.$suffix;  //执行压缩的文件图片

            //写入本地
            $request->file('goodspic')->move('./upload/',$filename);

            //实例化对象
//            $img = new Image;
//
//            // 调用方法
//            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

            //写入数据库
//            $res['goodspic'] = "/upload/s_".$filename;    //这是压缩后的
              $res['goodspic'] = "/upload/".$filename;    // 这是没压缩的

        }

        $res['goodstime'] = date('Y-m-d',time());

        $pro = DB::table('project_goods')->insert($res);

        if($pro){

            return redirect ('/Admin/goods/index')->with('success','添加成功');

        }else{

            return back()->with('error','添加失败');
        }
    }

    /**
     *   创建首页方法
     */
    public function getIndex(Request $request)
    {
        $res = DB::table('project_goods')
            ->where('goodsname','like','%'.$request->input('search').'%')
            ->where(function($query){
                $query->where('status','1');
            })->orderby('id','desc')->paginate($request->input('num',5));
        return view('/admin/goods/index',['row'=>$res,'request'=>$request->all()]);
    }

    /**
     *   创建禁用方法
     */
    public function getDelete()
    {
        $id = $_GET['id'];
        $res = DB::update("update project_goods set status= '0' where id = $id");
        if($res){

            return redirect('/Admin/goods/recycle')->with('success','禁用成功');

        }else{
            return back()->with('error','禁用失败');
        }
    }

    /**
     *   创建回收站方法
     */
    public function getRecycle(Request $request)
    {
        $res = DB::table('project_goods')
            ->where('goodsname','like','%'.$request->input('search').'%')
            ->where(function($query){
                $query->where('status','0');
            })
            ->paginate($request->input('num',5));
        return view('/admin/goods/recycle',['row'=>$res]);
    }

    /**
     *   c创建回收站启用方法
     */
    public function getReset()
    {
        $id = $_GET['id'];
        $res = DB::update("update project_goods set status='1' where id = $id");
        if($res){
            return redirect('/Admin/goods/index')->with('success','操作成功');
        }else{
            return back()->with('error','操作失败');
        }
    }

    /**
     *   编辑商品的界面
     */
    public function getEdit()
    {
        $id = $_GET['id'];
        $tid = DB::table('project_type')->lists('typename');
        $res = DB::table('project_goods')->where('id',$id)->first();
        return view('/admin/goods/edit',['row'=>$res],['tid'=>$tid]);
    }

    /**
     *   编辑商品的方法
     */
    public function postUpdate(Request $request)
    {
        $id = $_POST['id'];
        $updates = $request->except('_token','id');
        if($request->hasFile('goodspic')){
            //随机号
            $name = md5(rand(10000,99999).time());
            //后缀名
            $suffix = $request->file('goodspic')->getClientOriginalExtension();

            $filename = $name.'.'.$suffix;  //执行压缩的文件图片

            //写入本地
            $request->file('goodspic')->move('./upload/',$filename);

            //实例化对象
//            $img = new Image;
//
//            // 调用方法
//            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

            //写入数据库
//            $updates['goodspic'] = "/upload/s_".$filename;  //压缩后的有点模糊
            $updates['goodspic'] = "/upload/".$filename;     //未压缩的
        }
        $result = DB::table('project_goods')->where('id',$id)->update($updates);
        if($result){
            return redirect('/Admin/goods/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
     *   商品的删除方法
     */
    public function getDrop()
    {
        $id = $_GET['id'];
        $result = DB::table('project_goods')->where('id',$id)->delete();
        if($result){
            return redirect('/Admin/goods/recycle')->with('success','操作成功');
        }else{
            return back()->with('error','操作失败');
        }
    }

    /**
    *   添加商品详情图片页面
    */
    public function getAddpic()
    {
        $res = DB::table('project_goods')->get();

        return view('/Admin/goods/goodspic',['res'=>$res]);
    }

    /**
    *   添加商品方法
    */
    public function postGoodspic(Request $request)
    {

      
   
        $gid = $request->input('gid');

        $res = [];

        if($request->hasFile('picname1')) {
         
                //随机号
                $name = rand(10000,99999).time();

                //后缀名
                $suffix = $request->file('picname1')->getClientOriginalExtension();
              
                //图片文件
                $filename1 = $name.'.'.$suffix;   //执行图片压缩文件

                //存入本地
                $request->file('picname1')->move('./upload/',$filename1);

              //写入数据库
               $res[] = "/upload/".$filename1;
                
           }

        if($request->hasFile('picname2')) {
         
                //随机号
                $name = rand(10000,99999).time();

                //后缀名
                $suffix = $request->file('picname2')->getClientOriginalExtension();
              
                //图片文件
                $filename2 = $name.'.'.$suffix;   //执行图片压缩文件

                //存入本地
                $request->file('picname2')->move('./upload/',$filename2);

           
               //写入数据库
               $res[] = "/upload/".$filename2;

           } 

           foreach($res as $v=>$k) {

                $row['gid'] = $gid;

                $row['goodspicname'] = $k;

                $result = DB::table('project_goods_details')->insert($row);
           }

        
          if($result) {
             @unlink('./upload/'.$filename);
            return redirect('/Admin/goods/index')->with('success','添加成功');
          } else {
            return back()->with('error','添加失败');
         }
    }


}
